/**
 * Created by zqk on 2018/7/16.
 */
var currentTcId = 0;
var testCaseList;
var bt_list_edit = $('#bt_list_edit').attr('id');
var bt_list_add = $('#bt_list_add').attr('id');
var bt_list_delete = $('#bt_list_delete').attr('id');
var iProductId = addressUri.getParam('iProductId');
$(document).ready(function () {
    var form=new xhForm($('#xh_form'));
    testCaseList= $('#testCaseList');
    $('.xh-datagrid-toolbar').parseToolbar();
    //设置禁用
    $('#bt_list_edit').setButtonDisable('eu');
    $('#bt_list_delete').setButtonDisable('eu');
    form.parse();
    form.bindRequired($('#bt_form_add,#bt_form_edit'));//绑定事件
    testCaseList.datagrid({
        url: moduleUrl+'testcase/getTestCase?iProductId='+iProductId,
        method: 'post',
        rownumbers: true,
        pagination: true,
        pageSize: 50,
        pageList: [20,50,100,200],
        idField: 'iId',
        checkOnSelect: true,
        selectOnCheck: true,
        singleSelect: true,
        frozenColumns:[[
            {field:'ck',checkbox:true},
            {field:'iId',title:'iId',hidden:true},
            {field:'sCode',title:'编号',width:100,align:'left',halign:'center'},
        ]],
        columns:[[
            {field:'sModule',title:'模块',width:150,align:'left',halign:'center'},
            {field:'sSubmodule',title:'子模块',width:150,align:'left',halign:'center'},
            {field:'sFunctionPoint',title:'功能点',width:150,align:'left',halign:'center'},
            {field:'iLevel',title:'等级',width:80,align:'center',halign:'center',

                formatter: function(value,row,index){
                    if(value =='1'){
                        var str = '<div style = "background-color:#CCCCCC;color: #ffffff;height: 20px; line-height: 20px;">'+levels[value]+'</div>'
                        return str;
                    }else if(value =='2'){
                        var str = '<div style = "background-color:#EEEE00;color: #ffffff;height: 20px; line-height: 20px;">'+levels[value]+'</div>'
                        return str;
                    }else if(value =='3'){
                        var str = '<div style = "background-color:#ff1200;color: #ffffff;height: 20px; line-height: 20px;">'+levels[value]+'</div>'
                        return str;
                    }
                }

            },
            {field:'sPrecondition',title:'前置条件',width:150,align:'left',halign:'center'},
            {field:'sSteps',title:'操作步骤',width:150,align:'left',halign:'center'},
            {field:'sExpectedResult',title:'预期结果',width:150,align:'left',halign:'center'},
            // {field:'iActualResult',title:'实际结果',width:150,align:'left',halign:'center',
            //     formatter: function(value,row,index){
            //         if(value==1){
            //             return '成功';
            //         }else if(value==2){
            //             return '失败'
            //         }else{
            //             return '';
            //         }
            //     }
            // },
            {field:'sDefectNumber',title:'缺陷编号',width:150,align:'left',halign:'center'},
            // {field:'sUserName',title:'创建人',width:150,align:'left',halign:'center'},
            {field:'tCreateDate',title:'创建时间',width:150,align:'center',halign:'center'},
            {field:'tLastModifyDate',title:'最后编辑时间',width:150,align:'center',halign:'center'},
        ]],
        onCheck:function (rowIndex, rowData) {
            if(currentTcId==rowData['iId']){
                currentTcId = 0;
                testCaseList.datagrid('unselectRow',rowIndex);
                $('#bt_list_edit').setButtonDisable('eu');//编辑enable
                $('#bt_list_delete').setButtonDisable('eu');//删除enable
                return false;
            }else{
                currentTcId = rowData['iId'];
                $('#bt_list_edit').setButtonEnable('eu');//编辑enable
                $('#bt_list_delete').setButtonEnable('eu');//删除enable
            }
        },
    });

    $('#bt_list_search').click(function(){
        var sName = $('#sName').val();//获取input值
        testCaseList.datagrid('load',{sModule:sModule,iProjectId:iProjectId,iTestId:iTestId});
    });

    $('.toolbar a').click(function () {
        var eleid = $(this).attr('id');
        var obj=testCaseList.datagrid('getSelections')[0];
        if(xhExtra.buttonDisabled($(this))){
            return false;
        }
        if(eleid=='bt_list_add'){
            window.top.xhTop.showModal({
                title:'新增测试用例',
                content: '<iframe src="'+moduleUrl+'testcase/testCaseForm?iProductId='+iProductId+'"  style="width: 100%;border-style: none;padding: 0px;height: 350px"  scrolling="yes"></iframe>',
                button:'提交',
                backClose: false
            });
        } else if(eleid =='bt_list_delete'&&obj!=undefined){
            myWindow.initPopUp({title: '确认删除', text: '确定要删除该测试用例吗?'},function () {
                $.ajax({
                    url:moduleUrl+'testcase/delTestCaseById',
                    data:{iId:obj['iId']},
                    type:'POST',
                    success:function (data) {
                        window.top.xhTop.showSuccess(
                            data.msg
                        );
                        $('#bt_list_edit').setButtonDisable('eu');
                        $('#bt_list_delete').setButtonDisable('eu');
                        $('#bt_list_add').setButtonEnable('eu');
                        testCaseList.datagrid('reload');
                    },
                    error:function (data) {
                        window.top.xhTop.showError(
                            data.responseJSON.error,
                            data.responseJSON.msg
                        );
                    }
                });
            })
        }else if(eleid=='bt_list_import'){
            window.top.xhTop.showModal({
                title: '模板导入',
                content: '<iframe src="'+moduleUrl+'testcase/importForm?iProductId='+iProductId+'"  style="width: 100%;;border-style: none;padding: 0px;"  scrolling="yes"></iframe>',
                button:'提交',
                backClose: false,
            });

        }else if(eleid=='bt_list_edit'&&obj!=undefined){
            window.top.xhTop.showModal({
                title:'编辑测试用例',
                content: '<iframe src="'+moduleUrl+'testcase/testCaseForm?iId='+obj['iId']+'"  style="width: 100%;border-style: none;padding: 0px;height: 350px"  scrolling="yes"></iframe>',
                button:'提交',
                backClose: false,
            });

        }
    });

});

